package socket.tcp.demo1;

import java.io.*;
import java.net.Socket;

/**
 * @author 周宇
 * @create 2020-05-26 14:18
 */
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8888);
        //1
        OutputStream os = socket.getOutputStream();
        os.write("hello".getBytes());
        socket.shutdownOutput();//仅仅关闭输出流 并写一个结束标记，对socket没有任何影响
        //3
        //涉及中文不能用字节流 使用字符流
        // InputStream is = socket.getInputStream();
        // int b;
        // while ((b = is.read()) != -1) {
        //     System.out.println((char) b);
        // }
        BufferedReader br = new BufferedReader(new InputStreamReader((socket.getInputStream())));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
        os.close();
        socket.close();
    }
}
